package com.aiss.ws.httptools;

import android.util.Log;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 
 * @author 陈强
 *
 */
public class GetJSONObjectPostUtil {

	private HttpHandler<String> handler;
	/**
	 * 请求JSONObject
	 * @param url
	 * @param map
	 */
	public GetJSONObjectPostUtil(String url, HashMap<String, String> map, final GetJsonListener listener) {
		super();
		//添加必填参数
		RequestParams params = new RequestParams();
		Set<Entry<String, String>> set = map.entrySet();
		for (Entry<String, String> e : set) {
			params.addBodyParameter(e.getKey(), e.getValue());
		}
		
		handler = SingleInstanceUtils.getHttpUtilsInstance().send(HttpRequest.HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException error, String msg) {
						listener.onFailed(error, msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> result) {
						try {
							Log.v("TAG", "当前获取字符串"+result.result);
							JSONObject jsonObject = new JSONObject(result.result);
							listener.onSuccessed(jsonObject);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

				});
	}
	
	
	
	
	
	public HttpHandler<String> getHttpHandler(){
		return handler;
	}

}
